﻿CREATE PROCEDURE [dbo].[UpdatePostData]
	@TID BIGINT,
	@Domain NVARCHAR(255),
	@BannedBy NVARCHAR(255), 
	@Subreddit NVARCHAR(255), 
	@SelfTextHtml NVARCHAR(500), 
	@SelfText NVARCHAR(255), 
	@Likes INT, 
	@SecureMedia BIGINT, 
	@Saved BIT,
	@ID NVARCHAR(255),
	@SecureMediaEmbed BIGINT, 
	@Clicked BIT,
	@Stickied BIT,
	@Author NVARCHAR(255),
	@Score INT,
	@ApprovedBy NVARCHAR(255), 
	@Over18 BIT,
	@Hidden BIT,
	@Thumbnail NVARCHAR(500),
	@SubredditID NVARCHAR(255),
	@Edited BIT,
	@LinkFlairCssClass NVARCHAR(500), 
	@AuthorFlairCssClass NVARCHAR(500), 
	@Downs INT,
	@IsSelf BIT,
	@Permalink NVARCHAR(1000),
	@Name NVARCHAR(255),
	@Created DATETIME,
	@URL NVARCHAR(1000), 
	@AuthorFlairText NVARCHAR(500), 
	@Title NVARCHAR(1000),
	@CreatedUTC DATETIME,
	@LinkFlairText NVARCHAR(500), 
	@Ups INT,
	@NumComments INT,
	@NumReports INT,
	@Distinguished BIT,
	@MediaEmbed BIGINT,
	@Media BIGINT
AS
BEGIN
	BEGIN TRY
		SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
		SET NOCOUNT ON;

		DECLARE @datacounter INT;
		DECLARE @report XML;

		UPDATE [Raw].[vwPostData]
		SET Domain              = @Domain,
			BannedBy            = @BannedBy,
			Subreddit           = @Subreddit,
			SelfTextHtml        = @SelfTextHtml,
			SelfText            = @SelfText,
			Likes               = @Likes,
			SecureMedia         = @SecureMedia,
			Saved               = @Saved,
			ID                  = @ID,
			SecureMediaEmbed    = @SecureMediaEmbed,
			Clicked             = @Clicked,
			Stickied            = @Stickied,
			Author              = @Author,
			Score               = @Score,
			ApprovedBy          = @ApprovedBy,
			Over18              = @Over18,
			Hidden              = @Hidden,
			Thumbnail           = @Thumbnail,
			SubredditID         = @SubredditID,
			Edited              = @Edited,
			LinkFlairCssClass   = @LinkFlairCssClass,
			AuthorFlairCssClass = @AuthorFlairCssClass,
			Downs               = @Downs,
			IsSelf              = @IsSelf,
			Permalink           = @Permalink,
			Name                = @Name,
			Created             = @Created,
			URL                 = @URL,
			AuthorFlairText     = @AuthorFlairText,
			Title               = @Title,
			CreatedUTC          = @CreatedUTC,
			LinkFlairText       = @LinkFlairText,
			Ups                 = @Ups,
			NumComments         = @NumComments,
			NumReports          = @NumReports,
			Distinguished       = @Distinguished,
			MediaEmbed          = @MediaEmbed,
			Media               = @Media,
			VersionUser         = SYSTEM_USER,
			VersionDate         = GETDATE()
		WHERE DataID            = @TID

		/***  Logging  ***/
		SELECT @datacounter = @@ROWCOUNT;
		SET @report = (SELECT	'Data Created'	AS LogMessage,
								GETDATE()		AS LogTimestamp,
								@datacounter	AS RowsAffected
								FOR XML PATH('Notification'),
								TYPE,
								ROOT('Activity'));	

		EXEC dbo.CreateLog @LogType = 'UPDATE', @TableName = 'PostData', @Metadata = @report;
		RETURN 0
	END TRY
	BEGIN CATCH
		DECLARE @ErrorMessage NVARCHAR(4000);
		DECLARE @ErrorSeverity INT;
		DECLARE @ErrorState INT;
		DECLARE @ErrorTimeStamp DATETIME;
		DECLARE @ErrAction NVARCHAR(25);

		SELECT	@ErrorMessage   = ERROR_MESSAGE(),
				@ErrorSeverity  = ERROR_SEVERITY(),
				@ErrorState     = ERROR_STATE(),
				@ErrorTimeStamp = GETDATE(),
				@ErrAction      = 'UPDATE';

		SET @report = (SELECT	@ErrorMessage	AS ErrorMessage,
								@ErrAction	AS ErrorAction,
								@ErrorSeverity	AS ErrorSeverity,
								@ErrorState		AS ErrorState,
								@ErrorTimeStamp AS ErrorTimestamp 
								FOR XML PATH('Error'), 
								TYPE, 
								ROOT('ErrorLog'));		

		EXEC dbo.CreateErrorLog @ErrorType = 'ERROR', @ErrorAction = @ErrAction, @TableName = 'PostData', @Metadata = @report;
	END CATCH
END